package com.tms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tms.domain.Bill;
import com.tms.domain.BillResponse;
import com.tms.domain.req.BillReq;
import com.tms.mapper.develop.BillMapper;
import com.tms.service.BillService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 采购记录业务实现
 *
 * @author ozj
 */
@Service
public class BillServiceImpl implements BillService {

    @Autowired
    private BillMapper billMapper;

    @Override
    public BillReq create(BillReq billReq) {
        billMapper.insertSelective(billReq);
        return billReq;
    }

    @Override
    public void deleteById(Integer id) {
        billMapper.deleteByPrimaryKey(id);
    }


    @Override
    public BillReq updateById(BillReq req) {
        Bill bill = new Bill();
        BeanUtils.copyProperties(req, bill);
        int result = billMapper.updateByPrimaryKeySelective(bill);

        if (result == 0) {
            throw new RuntimeException("更新失败，更新结果数为0");
        }

        return req;
    }

    @Override
    public BillResponse findById(Integer id) {
        return billMapper.selectByPrimaryKeyOfResponse(id);
    }

    @Override
    public PageInfo<BillResponse> findAlls(int page, int pageSize, int status, int workcellId) {
        PageHelper.startPage(page, pageSize);
        List<BillResponse> responses = billMapper.selectAllsOfResponse(status, workcellId);
        return new PageInfo<>(responses);
    }

    @Override
    public void updateStatusById(BillResponse billResponse) {
        Bill bill = new Bill();
        BeanUtils.copyProperties(billResponse, bill);
        int result = billMapper.updateByPrimaryKeySelective(bill);
        if (result == 0) {
            throw new RuntimeException("更新失败，更新结果数为0");
        }

    }
}